package com.example.tibetanmedicinechest.controller;


import com.example.tibetanmedicinechest.model.Code;
import com.example.tibetanmedicinechest.model.Medicine;
import com.example.tibetanmedicinechest.model.Result;
import com.example.tibetanmedicinechest.service.IMedicineService;
import com.example.tibetanmedicinechest.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 编程笨蛋
 * @since 2023-08-12
 */
@RestController
@RequestMapping("/medicine")
public class MedicineController {
    @Autowired
    IMedicineService iMedicineService;
    @GetMapping
    public Result getAllMedicine(){
        List<Map<String, Object>> maps = iMedicineService.selectAllMedicine();
        return ResultUtil.getSuccess(maps);
    }

    @GetMapping("/queryMedicine/{keyword}")
    public Result queryMedicine(@PathVariable String keyword){
        List<Medicine> maps = iMedicineService.queryMedicine(keyword);
        if(maps.size()!=0){
            return ResultUtil.getSuccess(maps);
        } else {
            return ResultUtil.getError(Code.BUSINESS_ERR,"无搜索结果");
        }
    }

    @GetMapping("/selectById/{id}")
    public Result getMedicineById(@PathVariable Integer id){
        Medicine medicine = iMedicineService.selectById(id);
        if(medicine!=null){
            return ResultUtil.getSuccess(medicine);
        } else {
            return ResultUtil.getError(Code.BUSINESS_ERR,"该药品不存在");
        }
    }

}

